home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / intuisup.lha / Intuisup / source.lha / Texts / texts.c < prev    next >
C/C++ Source or Header  |  1992-04-08  |  10KB  |  435 lines

  1. /* $Revision Header *** Header built automatically - do not edit! ***********
  2.  *
  3.  *    (C) Copyright 1991 by Torsten Jürgeleit
  4.  *
  5.  *    Name .....: texts.c
  6.  *    Created ..: Thursday 19-Dec-91 16:46:55
  7.  *    Revision .: 2
  8.  *
  9.  *    Date        Author                 Comment
  10.  *    =========   ====================   ====================
  11.  *    21-Mar-92   Torsten Jürgeleit      flags for converting numbers
  12.  *    28-Dec-91   Torsten Jürgeleit      no complement if ri_ScreenDepth == 1
  13.  *                                         and not count gadget value text
  14.  *    19-Dec-91   Torsten Jürgeleit      Created this file!
  15.  *
  16.  ****************************************************************************
  17.  *
  18.  *    Text output and converting functions
  19.  *
  20.  * $Revision Header ********************************************************/
  21.  
  22.     /* Includes */
  23.  
  24. #include <exec/types.h>
  25. #include <exec/memory.h>
  26. #include <graphics/text.h>
  27. #include <intuition/intuition.h>
  28. #ifdef AZTEC_C
  29. #include <functions.h>   /* needed for Aztec C - prototypes and pragmas for all Amiga system functions */
  30. #endif
  31. #include <libraries/memwatch.h>   /* header file for memory debug link library (Fish 240) - AFTER functions.h */
  32. #include <string.h>
  33. #include "/render/render.h"
  34. #include "/language/language.h"
  35. #include "texts.h"
  36.  
  37.     /* Display texts described by text list */
  38.  
  39.    VOID
  40. display_texts(struct RenderInfo  *ri, struct Window  *win,
  41.              struct TextData  *td, SHORT hoffset, SHORT voffset,
  42.                          BYTE **language_text_array)
  43. {
  44.    if (ri && win && td) {
  45.       for ( ; td->td_Type != INTUISUP_DATA_END &&
  46.                   td->td_Type <= MAX_TEXT_DATA_TYPE; td++) {
  47.      BYTE *text;
  48.  
  49.      if (td->td_Type == TEXT_DATA_TYPE_TEXT) {
  50.         text = get_language_text(td->td_Text, language_text_array);
  51.      } else {
  52.         text = td->td_Text;
  53.      }
  54.      print_text(ri, win, text, td->td_LeftEdge + hoffset,
  55.                       td->td_TopEdge + voffset, td->td_Type,
  56.                          td->td_Flags, td->td_TextAttr);
  57.       }
  58.    }
  59. }
  60.     /* Print text and return width of text */
  61.  
  62.    USHORT
  63. print_text(struct RenderInfo  *ri, struct Window  *win, BYTE *text,
  64.            USHORT left_edge, USHORT top_edge, USHORT type, USHORT flags,
  65.                         struct TextAttr  *text_attr)
  66. {
  67.    USHORT width = 0;
  68.  
  69.    if (ri && (win || (flags & TEXT_DATA_FLAG_NO_PRINT)) &&
  70.                     (text || type != TEXT_DATA_TYPE_TEXT)) {
  71.       struct TextFont  *tf;
  72.  
  73.       /* If no text attr given then use screen text attr */
  74.       if (!text_attr || !(text_attr = ask_font(ri, text_attr))) {
  75.      text_attr = &ri->ri_TextAttr;
  76.       }
  77.  
  78.       /* Load font into memory */
  79.       if (tf = open_font(ri, text_attr)) {
  80.      struct IntuiText  itext, *it = &itext;
  81.      struct TextAttr   tattr, *ta = &tattr;
  82.      BYTE buffer[MAX_NUM_BUFFER_SIZE];
  83.  
  84.      /* Build text attr with style described by flags */
  85.      CopyMem((BYTE *)text_attr, (BYTE *)ta,
  86.                          (LONG)sizeof(struct TextAttr));
  87.      ta->ta_Style = FS_NORMAL;
  88.      if (flags & TEXT_DATA_FLAG_BOLD) {
  89.         ta->ta_Style |= FSF_BOLD;
  90.      }
  91.      if (flags & TEXT_DATA_FLAG_ITALIC) {
  92.         ta->ta_Style |= FSF_ITALIC;
  93.      }
  94.      if (flags & TEXT_DATA_FLAG_UNDERLINED) {
  95.         ta->ta_Style |= FSF_UNDERLINED;
  96.      }
  97.  
  98.      /* If number then convert it to text */
  99.      if (type != TEXT_DATA_TYPE_TEXT) {
  100.         USHORT conv_flags = 0;
  101.  
  102.         /* Calc convert flags */
  103.         if (flags & TEXT_DATA_FLAG_NUM_IDENTIFIER) {
  104.            conv_flags |= CONVERT_FLAG_IDENTIFIER;
  105.         }
  106.         if (flags & TEXT_DATA_FLAG_NUM_C_STYLE) {
  107.            conv_flags |= CONVERT_FLAG_C_STYLE;
  108.         }
  109.         if (flags & TEXT_DATA_FLAG_NUM_LEADING_ZEROES) {
  110.            conv_flags |= CONVERT_FLAG_LEADING_ZEROES;
  111.         }
  112.         if (flags & TEXT_DATA_FLAG_NUM_UPPER_CASE) {
  113.            conv_flags |= CONVERT_FLAG_UPPER_CASE;
  114.         }
  115.  
  116.         /* Convert number to text */
  117.         switch (type) {
  118.            case TEXT_DATA_TYPE_NUM_UNSIGNED_DEC :
  119.           convert_unsigned_dec((ULONG)text, &buffer[0], conv_flags);
  120.           break;
  121.  
  122.            case TEXT_DATA_TYPE_NUM_SIGNED_DEC :
  123.           convert_signed_dec((LONG)text, &buffer[0], conv_flags);
  124.           break;
  125.  
  126.            case TEXT_DATA_TYPE_NUM_HEX :
  127.           convert_hex((ULONG)text, &buffer[0], conv_flags);
  128.           break;
  129.  
  130.            case TEXT_DATA_TYPE_NUM_BIN :
  131.           convert_bin((ULONG)text, &buffer[0], conv_flags);
  132.           break;
  133.         }
  134.      }
  135.  
  136.      /* Get length of intui text */
  137.      it->IText     = (UBYTE *)(type == TEXT_DATA_TYPE_TEXT ? text :
  138.                                 &buffer[0]);
  139.      it->ITextFont = ta;
  140.      width         = IntuiTextLength(it);
  141.      if (!(flags & TEXT_DATA_FLAG_NO_PRINT)) {
  142.  
  143.         /* Init intui text */
  144.         it->LeftEdge = 0;
  145.         it->TopEdge  = 0;
  146.         it->BackPen  = ri->ri_BackPen;
  147.         it->NextText = NULL;
  148.  
  149.         /* Perform modifications indicated by flags */
  150.         if (flags & TEXT_DATA_FLAG_CENTER) {
  151.            left_edge = (win->Width - width) / 2;
  152.         } else {
  153.            if (flags & TEXT_DATA_FLAG_PLACE_LEFT) {
  154.           left_edge -= width;
  155.            }
  156.         }
  157.         if (flags & TEXT_DATA_FLAG_COLOR2) {
  158.            it->FrontPen = ri->ri_TextPen2;
  159.         } else {
  160.            it->FrontPen = ri->ri_TextPen1;
  161.         }
  162.         if ((flags & TEXT_DATA_FLAG_COMPLEMENT) &&
  163.                           (ri->ri_ScreenDepth > 1 ||
  164.                  (flags & INTERNAL_TEXT_DATA_FLAG_COUNT))) {
  165.            it->FrontPen ^= (1 << ri->ri_ScreenDepth) - 1;
  166.            it->BackPen  ^= (1 << ri->ri_ScreenDepth) - 1;
  167.         }
  168.         if (flags & TEXT_DATA_FLAG_BACK_FILL) {
  169.            it->DrawMode = JAM2;
  170.         } else {
  171.            it->DrawMode = JAM1;
  172.         }
  173.         if (!(flags & TEXT_DATA_FLAG_ABSOLUTE_POS) &&
  174.                (ri->ri_Flags & RENDER_INFO_FLAG_INNER_WINDOW)) {
  175.            if (!(flags & TEXT_DATA_FLAG_CENTER)) {
  176.           left_edge += ri->ri_WindowBorderLeft;
  177.            }
  178.            top_edge += ri->ri_WindowBorderTop;
  179.         }
  180.         PrintIText(win->RPort, it, (LONG)left_edge, (LONG)top_edge);
  181.      }
  182.      CloseFont(tf);
  183.       }
  184.    }
  185.    return(width);
  186. }
  187.     /* Convert num to string with unsigned dec value of num */
  188.  
  189. ULONG dec_table[MAX_DEC_NUM_DIGITS] = {   /* used also by gadgets.c */
  190.    1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1
  191. };
  192.  
  193.    USHORT
  194. convert_unsigned_dec(ULONG num, BYTE *buffer, USHORT flags)
  195. {
  196.    USHORT len = 0;
  197.  
  198.    if (!num) {
  199.  
  200.       /* Insert zero value */
  201.       if (!(flags & CONVERT_FLAG_LEADING_ZEROES)) {
  202.      if (buffer) {
  203.         *buffer++ = '0';
  204.      }
  205.      len++;
  206.       } else {
  207.      if (buffer) {
  208.         strcpy(buffer, "0000000000");
  209.         buffer += MAX_DEC_NUM_DIGITS;
  210.      }
  211.      len += MAX_DEC_NUM_DIGITS;
  212.       }
  213.    } else {
  214.       USHORT i = 0;
  215.  
  216.       /* Strip leading zeros */
  217.       if (!(flags & CONVERT_FLAG_LEADING_ZEROES)) {
  218.      for ( ; i < MAX_DEC_NUM_DIGITS; i++) {
  219.         if (num >= dec_table[i]) {
  220.            break;
  221.         }
  222.      }
  223.       }
  224.  
  225.       /* Convert num */
  226.       for ( ; i < MAX_DEC_NUM_DIGITS; i++) {
  227.      BYTE digit = num / dec_table[i];
  228.  
  229.      if (digit) {
  230.         num -= digit * dec_table[i];
  231.      }
  232.      if (buffer) {
  233.         *buffer++ = '0' + digit;
  234.      }
  235.      len++;
  236.       }
  237.    }
  238.    if (buffer) {
  239.       *buffer = '\0';
  240.    }
  241.    return(len);
  242. }
  243.     /* Convert num to string with signed dec value of num */
  244.  
  245.    USHORT
  246. convert_signed_dec(LONG num, BYTE *buffer, USHORT flags)
  247. {
  248.    USHORT len = 0;
  249.  
  250.    if (!num) {
  251.  
  252.       /* Insert zero value */
  253.       if (!(flags & CONVERT_FLAG_LEADING_ZEROES)) {
  254.      if (buffer) {
  255.         *buffer++ = '0';
  256.      }
  257.      len++;
  258.       } else {
  259.      if (buffer) {
  260.         strcpy(buffer, "0000000000");
  261.         buffer += MAX_DEC_NUM_DIGITS;
  262.      }
  263.      len += MAX_DEC_NUM_DIGITS;
  264.       }
  265.    } else {
  266.       USHORT i = 0;
  267.  
  268.       /* If num is negativ then prepend minus sign */
  269.       if (num < 0) {
  270.      if (buffer) {
  271.         *buffer++ = '-';
  272.      }
  273.      len++;
  274.      num = -num;
  275.       }
  276.  
  277.       /* Strip leading zeros */
  278.       if (!(flags & CONVERT_FLAG_LEADING_ZEROES)) {
  279.      for ( ; i < MAX_DEC_NUM_DIGITS; i++) {
  280.         if (num >= dec_table[i]) {
  281.            break;
  282.         }
  283.      }
  284.       }
  285.  
  286.       /* Convert num */
  287.       for ( ; i < MAX_DEC_NUM_DIGITS; i++, len++) {
  288.      BYTE digit = num / dec_table[i];
  289.  
  290.      if (digit) {
  291.         num -= digit * dec_table[i];
  292.      }
  293.      if (buffer) {
  294.         *buffer++ = '0' + digit;
  295.      }
  296.       }
  297.    }
  298.    if (buffer) {
  299.       *buffer = '\0';
  300.    }
  301.    return(len);
  302. }
  303.     /* Convert num to string with hex value of num */
  304.  
  305.    USHORT
  306. convert_hex(ULONG num, BYTE *buffer, USHORT flags)
  307. {
  308.    USHORT len = 0;
  309.  
  310.    /* Insert identifier */
  311.    if (flags & CONVERT_FLAG_IDENTIFIER) {
  312.       if (flags & CONVERT_FLAG_C_STYLE) {
  313.      if (buffer) {
  314.         if (flags & CONVERT_FLAG_UPPER_CASE) {
  315.            strcpy(buffer, "0X");
  316.         } else {
  317.            strcpy(buffer, "0x");
  318.         }
  319.         buffer += 2;
  320.      }
  321.      len += 2;
  322.       } else {
  323.          if (buffer) {
  324.         *buffer++ = '$';
  325.      }
  326.      len++;
  327.       }
  328.    }
  329.    if (!num) {
  330.  
  331.       /* Insert zero value */
  332.       if (!(flags & CONVERT_FLAG_LEADING_ZEROES)) {
  333.      if (buffer) {
  334.         *buffer++ = '0';
  335.      }
  336.      len++;
  337.       } else {
  338.      if (buffer) {
  339.         strcpy(buffer, "00000000");
  340.         buffer += MAX_HEX_NUM_DIGITS;
  341.      }
  342.      len += MAX_HEX_NUM_DIGITS;
  343.       }
  344.    } else {
  345.       ULONG mask = 0xf0000000;
  346.       SHORT i = (MAX_HEX_NUM_DIGITS - 1) * 4;
  347.  
  348.       /* Strip leading zeros */
  349.       if (!(flags & CONVERT_FLAG_LEADING_ZEROES)) {
  350.      for ( ; i >= 0; i -= 4, mask >>= 4) {
  351.         if ((num & mask) >> i) {
  352.            break;
  353.         }
  354.      }
  355.       }
  356.  
  357.       /* Convert num */
  358.       for ( ; i >= 0; i -= 4, mask >>= 4, len++) {
  359.      if (buffer) {
  360.         BYTE digit = (num & mask) >> i;
  361.  
  362.         if (flags & CONVERT_FLAG_UPPER_CASE) {
  363.            *buffer++ = (digit < 10 ? '0' : 'A' - 10) + digit;
  364.         } else {
  365.            *buffer++ = (digit < 10 ? '0' : 'a' - 10) + digit;
  366.         }
  367.      }
  368.       }
  369.    }
  370.    if (buffer) {
  371.       *buffer = '\0';
  372.    }
  373.    return(len);
  374. }
  375.     /* Convert num to string with bin value of num */
  376.  
  377.    USHORT
  378. convert_bin(ULONG num, BYTE *buffer, USHORT flags)
  379. {
  380.    USHORT len = 0;
  381.  
  382.    /* Insert identifier */
  383.    if (flags & CONVERT_FLAG_IDENTIFIER) {
  384.       if (buffer) {
  385.      *buffer++ = '%';
  386.       }
  387.       len++;
  388.    }
  389.    if (!num) {
  390.  
  391.       /* Insert zero value */
  392.       if (!(flags & CONVERT_FLAG_LEADING_ZEROES)) {
  393.      if (buffer) {
  394.         *buffer++ = '0';
  395.      }
  396.      len++;
  397.       } else {
  398.      if (buffer) {
  399.         strcpy(buffer, "00000000000000000000000000000000");
  400.         buffer += MAX_BIN_NUM_DIGITS;
  401.      }
  402.      len += MAX_BIN_NUM_DIGITS;
  403.       }
  404.    } else {
  405.       ULONG  mask = 0x80000000;
  406.       USHORT i = 0;
  407.  
  408.       /* Strip leading zeros */
  409.       if (!(flags & CONVERT_FLAG_LEADING_ZEROES)) {
  410.      for ( ; i < MAX_BIN_NUM_DIGITS; i++, mask >>= 1) {
  411.         if (num & mask) {
  412.            break;
  413.         }
  414.      }
  415.       }
  416.  
  417.       /* Convert num */
  418.       for ( ; i < MAX_BIN_NUM_DIGITS; i++, mask >>= 1, len++) {
  419.      if (num & mask) {
  420.         if (buffer) {
  421.            *buffer++ = '1';
  422.         }
  423.      } else {
  424.         if (buffer) {
  425.            *buffer++ = '0';
  426.         }
  427.      }
  428.       }
  429.    }
  430.    if (buffer) {
  431.       *buffer = '\0';
  432.    }
  433.    return(len);
  434. }
  435.